home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / dbleQR.cc < prev    next >
C/C++ Source or Header  |  1997-07-10  |  3KB  |  152 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "dbleQR.h"
  32. #include "f77-fcn.h"
  33. #include "lo-error.h"
  34. #include "mx-inlines.cc"
  35.  
  36. extern "C"
  37. {
  38.   int F77_FCN (dgeqrf, DGEQRF) (const int&, const int&, double*,
  39.                 const int&, double*, double*,
  40.                 const int&, int&); 
  41.  
  42.   int F77_FCN (dorgqr, DORGQR) (const int&, const int&, const int&,
  43.                 double*, const int&, double*, double*,
  44.                 const int&, int&);
  45. }
  46.  
  47. QR::QR (const Matrix& a, QR::type qr_type)
  48.   : q (), r ()
  49. {
  50.   init (a, qr_type);
  51. }
  52.  
  53. void
  54. QR::init (const Matrix& a, QR::type qr_type)
  55. {
  56.   int m = a.rows ();
  57.   int n = a.cols ();
  58.  
  59.   if (m == 0 || n == 0)
  60.     {
  61.       (*current_liboctave_error_handler) ("QR must have non-empty matrix");
  62.       return;
  63.     }
  64.  
  65.   int min_mn = m < n ? m : n;
  66.   Array<double> tau (min_mn);
  67.   double *ptau = tau.fortran_vec ();
  68.  
  69.   int lwork = 32*n;
  70.   Array<double> work (lwork);
  71.   double *pwork = work.fortran_vec ();
  72.  
  73.   int info = 0;
  74.  
  75.   Matrix A_fact;
  76.   if (m > n)
  77.     {
  78.       A_fact.resize (m, m);
  79.       A_fact.insert (a, 0, 0);
  80.     }
  81.   else
  82.     A_fact = a;
  83.  
  84.   double *tmp_data = A_fact.fortran_vec ();
  85.  
  86.   F77_XFCN (dgeqrf, DGEQRF, (m, n, tmp_data, m, ptau, pwork, lwork, info));
  87.  
  88.   if (f77_exception_encountered)
  89.     (*current_liboctave_error_handler) ("unrecoverable error in dgeqrf");
  90.   else
  91.     {
  92.       if (qr_type == QR::raw)
  93.     {
  94.       for (int j = 0; j < min_mn; j++)
  95.         {
  96.           int limit = j < min_mn - 1 ? j : min_mn - 1;
  97.           for (int i = limit + 1; i < m; i++)
  98.         A_fact.elem (i, j) *= tau.elem (j);
  99.         }
  100.  
  101.       r = A_fact;
  102.  
  103.       if (m > n)
  104.         r.resize (m, n);
  105.     }
  106.       else
  107.     {
  108.       volatile int n2;
  109.  
  110.       if (qr_type == QR::economy && m > n)
  111.         {
  112.           n2 = n;
  113.           r.resize (n, n, 0.0);
  114.         }
  115.       else
  116.         {
  117.           n2 = m;
  118.           r.resize (m, n, 0.0);
  119.         }
  120.  
  121.       for (int j = 0; j < n; j++)
  122.         {
  123.           int limit = j < min_mn-1 ? j : min_mn-1;
  124.           for (int i = 0; i <= limit; i++)
  125.         r.elem (i, j) = tmp_data[m*j+i];
  126.         }
  127.  
  128.       lwork = 32*m;
  129.       work.resize (lwork);
  130.       double *pwork = work.fortran_vec ();
  131.  
  132.       F77_XFCN (dorgqr, DORGQR, (m, m, min_mn, tmp_data, m, ptau,
  133.                      pwork, lwork, info));
  134.  
  135.       if (f77_exception_encountered)
  136.         (*current_liboctave_error_handler)
  137.           ("unrecoverable error in dorgqr");
  138.       else
  139.         {
  140.           q = A_fact;
  141.           q.resize (m, n2);
  142.         }
  143.     }
  144.     }
  145. }
  146.  
  147. /*
  148. ;;; Local Variables: ***
  149. ;;; mode: C++ ***
  150. ;;; End: ***
  151. */
  152.